import axios from 'axios'
import { Message } from 'element-ui'

// 响应拦截
axios.interceptors.response.use(success => {
  console.log('axios success')
  if (success.status === 202) {
    Message({
      type: 'warning',
      message: '警告 : ' + success.data
    })
  }
  return success
}, error => {
  console.log(error)
  Message({
    type: 'error',
    message: '失败 : ' + error
  })
  return error
})

let base = ''

// 传送JSON格式的POST请求
export const post = (url, params) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: `${base}${url}`,
      data: params
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

// get请求
export const get = (url) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: `${base}${url}`
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}

// 发送delete请求
export const deleteData = (url, params) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'delete',
      url: `${base}${url}`,
      data: params.toString()
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
